import { Component, OnInit, Input, EventEmitter, Output, Host, Inject, forwardRef, ViewChild, ElementRef, AfterViewInit } from '@angular/core';
import { HttpModule } from '@angular/http';
import { HttpClientModule, HttpClient, HttpErrorResponse } from '@angular/common/http';
import { RouterModule, Router, ActivatedRoute } from '@angular/router';
import { DragulaService } from 'ng2-dragula';
import { Service } from '../../../server/server';
import { Pagination } from '../../../public/serve/pcrudInfo';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';

@Component({
  selector: 'app-role-roleby-left',
  templateUrl: './role-roleby-left.component.html'
})
export class RoleRolebyLeftComponent implements OnInit {

  constructor(private server: Service, private tableListService: CudTableService, private dragulaService: DragulaService) { }
  ngOnInit() {
    //接受角色Id
    this.rolebyeventEmit = this.server.rolebyeventEmit.subscribe((value: any) => {
      this.rolebyuser(this.pageNumber, this.pageSize, "0", 0);
      this.searchData.queryCriteria = '';
    });
    //刷新
    this.rolebylefteventEmit = this.server.rolebylefteventEmit.subscribe((value: any) => {
      if (value) {
        //console.log("left");
        this.rolebyuser(this.pageNumber, this.pageSize, "0", 0);
        this.searchData.queryCriteria = "";
      }
    });
    this.rolebyrighteventEmit = this.server.rolebyrighteventEmit.subscribe((value: any) => {
      if (value) {
        //console.log("left");
        this.rolebyuser(this.pageNumber, this.pageSize, "0", 0);
        this.searchData.queryCriteria = "";
      }
    });

  }
  //取消订阅
  public rolebyeventEmit: any;
  public rolebylefteventEmit: any;
  public rolebyrighteventEmit: any;
  ngOnDestroy() {
    if (this.rolebyeventEmit || this.rolebylefteventEmit || this.rolebyrighteventEmit) {
      this.rolebyeventEmit.unsubscribe();
      this.rolebylefteventEmit.unsubscribe();
      this.rolebyrighteventEmit.unsubscribe();
    }
  }

  //   private hasClass(el:any, name:string):any {
  //     return new RegExp('(?:^|\\s+)' + name + '(?:\\s+|$)').test(el.className);
  //   }

  //   private addClass(el:any, name:string):void {
  //     if (!this.hasClass(el, name)) {
  //       el.className = el.className ? [el.className, name].join(' ') : name;
  //     }
  //   }

  //   private removeClass(el:any, name:string):void {
  //     if (this.hasClass(el, name)) {
  //       el.className = el.className.replace(new RegExp('(?:^|\\s+)' + name + '(?:\\s+|$)', 'g'), '');
  //     }
  //   }

  //   private onDrag(args:any):void {
  //     let [e] = args;
  //     this.removeClass(e, 'ex-moved');
  //   }

  //   private onDrop(args:any):void {
  //     let [e] = args;
  //     this.addClass(e, 'ex-moved');
  //   }

  //   private onOver(args:any):void {
  //     let [el] = args;
  //     this.addClass(el, 'ex-over');
  //   }

  //   private onOut(args:any):void {
  //     let [el] = args;
  //     this.removeClass(el, 'ex-over');
  //   }

  //表格
  public rolebyUserData: any[] = [];
  public rolebyuser(pageNumber, pageSize, id, queryType): void {
    let url = this.server.baseUrl+'admin/roles/getleftUserList';
    let data = new Pagination(pageNumber, pageSize, id, queryType);
    this.tableListService.postRequest(url, data).then((data: any) => {
      this.rolebyUserData = data.result;
      this.totalItems = data.length;
      this.clear();
      this.outselectedItems.emit(this.selectedItems);
      this.outselectedItemsName.emit(this.selectedItemsName);
      //console.log(data)
    }).catch();
  }

  //分页
  public totalItems: any;
  tableNumber: number = 0;
  // 当前页（默认为第一页）
  public pageNumber: number = 1;
  // 每页显示多少条数据（默认每页显示20条）
  public pageSize: number = 20;
  // id
  public id: string = '0';
  // 分页
  public pagination: Pagination;
  isLoadTableFromPaginationHandler(pagination: Pagination) {
    this.pagination = pagination;

    this.pageNumber = this.pagination[0].pageNumber;
    this.pageSize = this.pagination[0].pageSize;
    this.id = this.pagination[0].id;

    this.tableNumber = (pagination[0].pageNumber - 1) * pagination[0].pageSize;

    this.rolebyuser(this.pageNumber, this.pageSize, this.id, 0);
  }
  //全选，单选
  public selectedItems: string[] = [];
  public selectedItemsName: string[] = [];

  @Output() outselectedItems = new EventEmitter();
  @Output() outselectedItemsName = new EventEmitter();
  @Output() outdouble = new EventEmitter();
  //发射双击数据
  public doublechange(id, name) {
    let val = { id: id, name: name }
    this.outdouble.emit(val);
    //this.server.prevent = true;
  }

  public getSysVal(userId, userName) {
    //console.log(userId,userName)
    let index = this.selectedItems.findIndex(id => userId == id)
    let indexname = this.selectedItemsName.findIndex(name => userName == name)
    let exist = index != -1
    let existName = indexname != -1
    if (exist) {
      this.selectedItems.splice(index, 1)
    } else {
      this.selectedItems.push(userId)
    }
    if (existName) {
      this.selectedItemsName.splice(indexname, 1)
    } else {
      this.selectedItemsName.push(userName)
    }
    if (this.selectedItems && this.selectedItemsName) {
      this.outselectedItems.emit(this.selectedItems);
      this.outselectedItemsName.emit(this.selectedItemsName);
    }
    //console.log(this.selectedItems)
    //console.log(this.selectedItemsName)
  }

  public isSelected(userId) {
    this.selectedItems = this.selectedItems ? this.selectedItems : [];
    return this.selectedItems.findIndex(id => userId == id) != -1;
  }

  public selectAll() {
    if (this.selectedItems.length == this.rolebyUserData.length) {
      this.clear();
    } else {
      this.clear();
      for (let each of this.rolebyUserData) {
        this.selectedItems.push(each.userId)
      }
      for (let each of this.rolebyUserData) {
        this.selectedItemsName.push(each.userName)
      }
    }
    if (this.selectedItems && this.selectedItemsName) {
      this.outselectedItems.emit(this.selectedItems);
      this.outselectedItemsName.emit(this.selectedItemsName);
    }

  }

  clear() {
    this.selectedItems = []
    this.selectedItemsName = []
  }

  //搜索
  public searchData: any = {
    queryCriteria: "",
    queryType: 3,
    pageNumber: 1,
    pageSize: 20,
  };
  public search() {
    let data = this.searchData = {
      queryCriteria: this.searchData.queryCriteria,
      queryType: 3,
      pageNumber: 1,
      pageSize: 20,
    }
    //console.log(data)
    let url = this.server.baseUrl+'admin/roles/getleftUserList';
    this.tableListService.postRequest(url, data).then((data: any) => {
      //console.log(data)
      this.rolebyUserData = data.result;
      this.totalItems = data.length;
      this.clear();
      this.outselectedItems.emit(this.selectedItems);
      this.outselectedItemsName.emit(this.selectedItemsName);
    }).catch();
  }
  //响应搜索
  changesearch() {
    this.search();
  }

  private onDropModel(args) {
    let [el, target, source] = args;
    // do something else
    alert(1)
  }

  private onRemoveModel(args) {
    let [el, source] = args;
    // do something else
    alert(1)
  }
}
